package com.jz.planclass.service.impl;

import com.jz.planclass.mapper.PlanClassMapper;
import com.jz.planclass.model.*;
import com.jz.planclass.service.PlanClassService;
import com.jz.util.AllRecordsVo;
import com.jz.util.ResultBuildVo;
import com.jz.util.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

/**
 * Created by WISE on 2021/12/27.
 */
@Service
public class PlanClassServiceImpl implements PlanClassService {

    @Autowired
    private PlanClassMapper planClassMapper;

    @Override
    public ResultVo queryPage(PlanClassSelectVo planClassSelectVo) {
//1.查询数据
        List<PlanClassVo> planClassVoList = planClassMapper.queryData(planClassSelectVo);
//2.查询数量
        int count = planClassMapper.quertCount(planClassSelectVo);
//3.最终把数据返回
        AllRecordsVo allRecordsVo = new AllRecordsVo();
        allRecordsVo.setPageSize(planClassSelectVo.getPageSize());
        allRecordsVo.setDataList(planClassVoList);
        allRecordsVo.setPageIndex(planClassSelectVo.getPageIndex());
        allRecordsVo.setTotalSize(count);
        allRecordsVo.countPage(count);

        return ResultBuildVo.success(allRecordsVo);
    }

    @Override
    public ResultVo addPlanClass(PlanClassAddVo planClassAddVo) {
//验证编码是否存在
        PlanClassVo planClassVo = planClassMapper.queryByCode(planClassAddVo.getCode());
        if (planClassVo != null) {

            return ResultBuildVo.error("编码已经存在");
        }
        PlanClassVo planClassVo1 = planClassMapper.queryByName(planClassAddVo.getName());
        if (planClassVo1 != null) {

            return ResultBuildVo.error("名称已经存在");
        }
        Date nowtime = new Date();
        PlanClassVo classVo = new PlanClassVo();
        BeanUtils.copyProperties(planClassAddVo, classVo);
        classVo.setCreatTime(nowtime);
        classVo.setCreatName("通过session获得用户名称");
        planClassMapper.add(classVo);
        return  ResultBuildVo.success();

}

    @Override
    public ResultVo queryById(Integer id) {

      PlanClassVo planClassVo =  planClassMapper.queryById(id);
        return ResultBuildVo.success(planClassVo);
    }

    @Override
    public ResultVo updatePlanClass(@Valid PlanClassUpdateVo planClassUpdateVo) {


        return null;
    }

    @Override
    public ResultVo updateStatus(@Valid PlanClassUpdateStatusVo planClassUpdateStatusVo) {
        PlanClassVo planClassVo = planClassMapper.queryById(planClassUpdateStatusVo.getId());

        if (planClassVo == null) {
            return ResultBuildVo.error("修改对象不存在");
        }
        Date nowDate = new Date();
        PlanClassVo planClassVo1 = new PlanClassVo();
        BeanUtils.copyProperties(planClassUpdateStatusVo, planClassVo1);
        planClassVo1.setUpdateTime(nowDate);
        planClassVo1.setUpdateName("通过Session拿到当前用户名称");
        planClassMapper.updateStatus(planClassVo1);

        return ResultBuildVo.success();
    }

}
